const path = require('path')
const nodeExternals = require('webpack-node-externals')
const webpack = require('webpack')

function resolve(dir) {
  return path.join(process.cwd(), '.', dir)
}
module.exports = {
  /*
  ** Headers of the page
  */
  head: {
    title: '容器云-开发平台',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: 'Nuxt.js project' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
    // script: [
    //   { src: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js' }
    // ]
  },
  /*
  ** Global CSS
  */
  css: ['~assets/css/main.css'],
  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#3B8070' },
  /*
  ** plugins
  */
  plugins: [{
    src: '~plugins/zlst-sdk.js',
    ssr: true
  }],
  /**
   * 路由配置
   */
  router: {
    middleware: 'auth'
  },
  /*
   ** Build configuration
   */
  build: {
    analyze: false,  // 开启生成代码分析
    render: ['@zlst/f2eSDK'],
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        F2ESDK_CONFIG: '~/plugins/zlst-config.js'
      })
    ],
    // babel配置
    babel: {
      'presets': [
        ['env', {
          'modules': false,
          'targets': {
            'browsers': ['> 1%', 'last 2 versions', 'not ie <= 8']
          }
        }],
        'stage-2'
      ],
      'plugins': ['transform-vue-jsx', 'transform-runtime', 'syntax-dynamic-import'],
      'env': {
        'test': {
          'presets': ['env', 'stage-2'],
          'plugins': ['transform-vue-jsx', 'istanbul']
        }
      }
    },
    loaders: [{
      test: /\.(scss)$/,
      use: [{
        loader: 'style-loader'
      }, {
        'loader': 'css-loader',
        'options': {
          'sourceMap': true
        }
      },
      {
        'loader': 'sass-loader',
        'options': {
          'includePaths': [
            resolve('node_modules/@zlst/f2eSDK/zlst/style')
          ],
          'sourceMap': true
        }
      }]
    }, {
      test: /\.(sass)$/,
      use: [{
        loader: 'style-loader'
      }, {
        'loader': 'css-loader',
        'options': {
          'sourceMap': true
        }
      },
      {
        'loader': 'sass-loader',
        'options': {
          indentedSyntax: true,
          'includePaths': [
            resolve('node_modules/@zlst/f2eSDK/zlst/style')
          ],
          'sourceMap': true
        }
      }]
    }],
    /*
     ** Run ESLINT on save
     */
    extend(config, ctx) {
      const babelLoader = config.module.rules.find((rule) => rule.loader === 'babel-loader')
      babelLoader.exclude = function (modulePath) {
        // babel编译node_modules目录下的f2eSDK目录，去除（f2eSDK目录下的node_modules，@riophae）不编译
        return /node_modules/.test(modulePath) && (!/f2eSDK/.test(modulePath) || /f2eSDK.*node_modules/.test(modulePath) || /f2eSDK.*@riophae/.test(modulePath))
      }
      // @zlst中scss文件处理 @import
      const vueLoader = config.module.rules.find((rule) => rule.loader === 'vue-loader')
      let sassArr = vueLoader.options.loaders.sass
      for (let index = 0; index < sassArr.length; index++) {
        const element = sassArr[index]
        if (element.loader === 'sass-loader') {
          element.options = { indentedSyntax: true, includePaths: [resolve('node_modules/@zlst/f2eSDK/zlst/style')] }
        }
      }
      let scssArr = vueLoader.options.loaders.scss
      for (let index = 0; index < scssArr.length; index++) {
        const element = scssArr[index]
        if (element.loader === 'sass-loader') {
          element.options = { includePaths: [resolve('node_modules/@zlst/f2eSDK/zlst/style')] }
        }
      }

      config.module.rules.find((rule) => {
        if (rule.test.toString().indexOf('(png|jpe?g|gif|svg)') !== -1) {
          rule.exclude = [resolve('icons')]
          config.module.rules.push({
            test: /\.svg$/,
            loader: 'svg-sprite-loader',
            include: [resolve('icons')],
            options: {
              symbolId: 'icon-[name]'
            }
          })
        }
      })

      // 服务端不包含@zlst 后台文件
      if (ctx.isServer) {
        config.externals = [
          nodeExternals({
            whitelist: [/f2eSDK/, /\.css$/]
          })
        ]
      }
      if (ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    }
  }
}
